#!/bin/sh

if [ $# -lt 1 ]; then
    echo "Error==>Need args,eg fxcore.x64.mt.25581.1599"
    exit 1
fi

FXCORE_VERSION=$1
FXCORE_SPEC_DEFAULT_NAME=$1.spec

# fetch version
fxcore_paramter=$1
IFS='.'
fxcore_list=($fxcore_paramter)
fx_version_fxcore=${fxcore_list[3]}
fx_version_fpdfapi=${fxcore_list[4]}

if [ ! -n "$fx_version_fxcore" ]; then
 echo "Error===>Does not have fxcore version value"
 exit
fi

if [ ! -n "$fx_version_fpdfapi" ]; then
 echo "Error===>Does not have fpdfapi version value"
 exit
fi

FXCORE_DIGITAL_VERSION=$fx_version_fxcore.$fx_version_fpdfapi
#FXCORE_DIGITAL_VERSION=${FXCORE_VERSION:6}
echo "FXCORE_DIGITAL_VERSION: $FXCORE_DIGITAL_VERSION"

if [ -n "$2" ]; then
 echo "#2 value: $2"
 FXCORE_SPEC_PATH=$2
else
 echo "#2 value null,used default"
 FXCORE_SPEC_PATH=./$FXCORE_SPEC_DEFAULT_NAME
fi

if [ -f "$FXCORE_SPEC_PATH" ]; then
    FXCORE_SPEC_PATH_BAK=$FXCORE_SPEC_PATH.bak
    echo "bakup spec path: $FXCORE_SPEC_PATH_BAK"
    mv $FXCORE_SPEC_PATH $FXCORE_SPEC_PATH_BAK
    if [ $? -ne 0 ]; then
      echo "backup failure,delete it now.."
      rm -rf $FXCORE_SPEC_PATH_BAK
    fi
else
   echo "No Find spec file: $FXCORE_SPEC_PATH,touch it now"
   ##touch $FXCORE_SPEC_PATH
fi

echo "Name:$FXCORE_VERSION" >> $FXCORE_SPEC_PATH
echo "Version:1.$FXCORE_DIGITAL_VERSION" >> $FXCORE_SPEC_PATH
echo 'Release:1' >> $FXCORE_SPEC_PATH
echo 'Summary:foxit core pdf development library' >> $FXCORE_SPEC_PATH
echo 'Group:Development/Libraries' >> $FXCORE_SPEC_PATH
echo 'License:Foxit Commercial License' >> $FXCORE_SPEC_PATH
echo 'URL:www.foxitcloud.com'  >> $FXCORE_SPEC_PATH
echo 'Source:fxcore.tar.gz'  >> $FXCORE_SPEC_PATH
echo 'BuildRequires:/usr/bin/rpmbuild'  >> $FXCORE_SPEC_PATH
echo 'BuildRoot:%{_tmppath}/%{name}-%{version}-%{release}-root'  >> $FXCORE_SPEC_PATH
echo '%description'  >> $FXCORE_SPEC_PATH
echo 'foxit core pdf development library' >> $FXCORE_SPEC_PATH

echo '%define    rpm_sources_name       SOURCES' >> $FXCORE_SPEC_PATH
echo '%define    user_path_home         /usr/local' >> $FXCORE_SPEC_PATH
echo '%define    fxcore_unique_name     fxcore' >> $FXCORE_SPEC_PATH
echo '%define    fxcore_unique_path     /usr/local/fxcore' >> $FXCORE_SPEC_PATH
echo '%define    fxcore_tar_gz_name     fxcore.tar.gz' >> $FXCORE_SPEC_PATH

echo '%prep' >> $FXCORE_SPEC_PATH
echo 'cd $RPM_BUILD_DIR' >> $FXCORE_SPEC_PATH
echo 'tar xzvf ../%{rpm_sources_name}/%{fxcore_tar_gz_name} -C $RPM_BUILD_DIR' >> $FXCORE_SPEC_PATH

echo '%build' >> $FXCORE_SPEC_PATH

echo '%install' >> $FXCORE_SPEC_PATH
echo 'rm -rf $RPM_BUILD_ROOT/* '  >> $FXCORE_SPEC_PATH
echo 'mkdir -p $RPM_BUILD_ROOT%{user_path_home}' >> $FXCORE_SPEC_PATH
echo 'cp  -Rf $RPM_BUILD_DIR/%{fxcore_unique_name}  $RPM_BUILD_ROOT%{fxcore_unique_path}' >> $FXCORE_SPEC_PATH

echo "%clean" >> $FXCORE_SPEC_PATH
echo 'rm -rf $RPM_BUILD_ROOT' >> $FXCORE_SPEC_PATH
echo 'rm -rf $RPM_BUILD_DIR/%{fxcore_unique_name}' >> $FXCORE_SPEC_PATH
echo 'rm -rf $RPM_BUILD_DIR/%{name}' >> $FXCORE_SPEC_PATH

echo '%files' >> $FXCORE_SPEC_PATH
echo '%defattr(-,root,root)' >> $FXCORE_SPEC_PATH
echo '%{fxcore_unique_path}' >> $FXCORE_SPEC_PATH

##echo '%changelog' >> $FXCORE_SPEC_PATH
##echo '* Mon August 4 2014 Michael Lin' >> $FXCORE_SPEC_PATH

- Added README file (#42).
if [ -f "$FXCORE_SPEC_PATH_BAK" ]; then
  echo "exist backup file,restore it now"
  mv $FXCORE_SPEC_PATH_BAK $FXCORE_SPEC_PATH
fi

echo "3Q,Create new spec file success: $FXCORE_SPEC_PATH"
exit 0


